package br.indt.framework.ui.common;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Display a alert message over current screen.	 
 *
 */
public class Alert extends Displayable {
	
	private Label label = null;
	private int startY = 0;
	private int labelStartX = 0;
	private int drawHeight = 0;
	private Image image = null;
	private static final int IMAGE_MARGIN = 5;
	private Button button = null;

	private int buttonStartX = 0;
	private int buttonStartY = 0; 
	
	/**
	 * Constructor 
	 * 
	 * @param width
	 * @param height
	 * @param listener
	 */
	public Alert(int width, int height, ButtonActionListener listener) {
		super("Alert");
		this.width = width;
		this.height = height;
		
		label = new Label("alertLabel", "Please Wait");
		label.setMargin(0, 5, 0, 5);
		add(label);
				
		button = new Button("button", "Ok", null);
		button.setMargin(20, 3, 20, 3);
		button.setButtonActionListener(listener);
		add(button);
				
		calculateDrawingCoordinates();
	}

	/**
	 * Return the reference to "Ok" button instance.
	 * 
	 * @return
	 */
	public Button getButton() {
		return button;
	}
	
	/**
	 * Defines the text to be showed on component
	 * 
	 * @param text
	 * @param image
	 */
	public void setText(String text, Image image) {
		label.setText(text);
		this.image = image;
		calculateDrawingCoordinates();
	}
	
	/**
	 * Defines the margins of component.
	 * 
	 * @param left
	 * @param top
	 * @param right
	 * @param bottom
	 */
	public void setMargin(int left, int top, int right, int bottom) {
		super.setMargin(left, top, right, bottom);
		label.setWidth(width - (leftMargin + rightMargin));
		calculateDrawingCoordinates();
	}

	/**
	 * This method is called by main ui thread to paint the component.
	 * 
	 * @param g
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public void paint(Graphics g, int x, int y, int width, int height) {
		width -= (leftMargin + rightMargin);
		x += leftMargin;
		y = startY;
		
		g.setColor(0);
		g.drawRoundRect(x, y, width, drawHeight, 20, 20);
		g.setColor(0xEEEEEE);
		g.fillRoundRect(x + 1, y + 1, width - 1, drawHeight -1 , 20, 20);
		
		if(image != null) {
			y += IMAGE_MARGIN;
			g.drawImage(image,leftMargin + (width / 2), y, Graphics.TOP | Graphics.HCENTER);
			y += image.getHeight() + IMAGE_MARGIN;
		}
		
		label.paint(g, labelStartX, y, width, height);
		y += label.getHeight();
		
		button.paint(g, buttonStartX, y, width, height);
	}
	
	private void calculateDrawingCoordinates() {
		int drawWidth = width - (leftMargin + rightMargin);
		drawHeight = label.getHeight() + ((image != null) ? image.getHeight()  + (IMAGE_MARGIN * 2) : 0) + button.getHeight();
		startY = height - bottomMargin - drawHeight;
		labelStartX = leftMargin + ((drawWidth - label.getActualWidth()) /2);
		buttonStartX = leftMargin + ((drawWidth - button.getWidth()) /2);
		buttonStartY = startY + ((image != null) ? image.getHeight() + (IMAGE_MARGIN * 2) : 0) + label.getHeight();
	}
	
	protected Widget getWidgetAt(int x, int y) {
		if(x >= buttonStartX && x <= buttonStartX + button.getWidth()
				&& y >= buttonStartY && y <= buttonStartY + button.getHeight()) {
			return button;
		}
		return null;
	}
	
}
